/**
 *
 */
package jp.seraph.same.view;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;

import jp.seraph.jsade.model.Align;
import jp.seraph.same.controller.PoseController;
import jp.seraph.same.model.JointModel;

/**
 * 一つのジョイントの選択状態を管理するラジオボタン
 * 
 */
public class JointRadioButton extends JRadioButton {
    private JointRadioButton(JointModel aModel, PoseController aController) {
        super(aModel.getJointName());
        mModel = aModel;
        mController = aController;

        this.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent aActionevent) {
                mController.selectJoint(mModel.getJointName());
            }
        });
    }

    public static JointRadioButton create(Container aContainer, JointModel aModel, PoseController aController) {
        if (!GROUP_MAP.containsKey(aContainer)) {
            synchronized (SYNC) {
                if (!GROUP_MAP.containsKey(aContainer)) {
                    GROUP_MAP.put(aContainer, new ButtonGroup());
                }
            }
        }

        JointRadioButton tResult = new JointRadioButton(aModel, aController);
        GROUP_MAP.get(aContainer).add(tResult);

        return tResult;
    }

    static {
        GROUP_MAP = new HashMap<Container, ButtonGroup>();
        SYNC = new Object();
    }

    private PoseController mController;
    private JointModel mModel;
    private static Map<Container, ButtonGroup> GROUP_MAP;
    private static Object SYNC;
    private static final long serialVersionUID = 10L;

    public JointModel getJointModel() {
        return mModel;
    }

    public Align getAlign() {
        return mModel.getJointAlign();
    }

    public int getOrder() {
        return mModel.getJointOrder();
    }
}
